package com.atom.process.cnmuseum.bm.dao.task;


import com.atom.common.dao.AvensCommonDao;
import com.atom.common.exception.AvensNullPointerException;
import com.atom.common.mb.entity.gl.GcxxDO;
import com.atom.common.remote.user.UserUtil;
import com.atom.process.cnmuseum.bm.entity.task.BmGcxxTaskDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BmGcxxTaskDao extends AvensCommonDao<BmGcxxTaskDO> {

    public BmGcxxTaskDO findByTaskId(String taskId) {
        List<BmGcxxTaskDO> list = super.findByProperty("taskId", taskId);
        if (list == null || list.size() == 0) {
            throw new AvensNullPointerException("TaskDO is null where taskId = " + taskId);
        } else {
            return list.get(0);
        }
    }

    public BmGcxxTaskDO findLastByProcessId(String taskId) {
        List<BmGcxxTaskDO> list = super.findByPropertyOrderBy("processId", taskId, "id");
        if (list == null || list.size() == 0) {
            throw new AvensNullPointerException("TaskDO is null where taskId = " + taskId);
        } else {
            return list.get(list.size() - 1);
        }
    }


    public Integer createADO(UserUtil user, String processInstanceId, String taskId, GcxxDO gcxx) {
        BmGcxxTaskDO ado = new BmGcxxTaskDO();
        BeanUtils.copyProperties(gcxx, ado, "id");
        ado.setProcessId(processInstanceId);
        ado.setTaskId(taskId);
        save(ado);
        return ado.getId();
    }

}
